import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import { Config } from '../../providers/config';
import { Common } from '../../providers/common';
import { App, AlertController } from 'ionic-angular';
import _ from 'lodash';

@Component({
	selector: 'page-archive',
	templateUrl: 'archive.html'
})
export class ArchivePage {
	moa_page_name: any = "page-archive";
  moa_page_explain = "文档中心";

	listData = [];
	constructor(
		public appCtrl: App,
		public alertCtrl: AlertController,
		public config: Config,
		public common: Common,
		public storage: Storage
	) {
		this.initList()
	}

	initList() {
		this.storage.get('open_file_history').then((fileList) => {
			this.listData = fileList ? _.reverse(fileList) : this.listData;
		});
	}

	cleanHistory() {
		let that = this;
		let prompt = this.alertCtrl.create({
			title: '提示',
			message: "是否清除所有文件浏览记录？",
			buttons: [
				{
					text: '取消',
					handler: data => {
						console.log('Cancel clicked');
					}
				},
				{
					text: '确认',
					handler: data => {
						that.common.removeDownloadAttach();
						that.listData = [];
						// that.storage.set('open_file_history', []).then(() => {
						// 	that.listData = [];
						// });
					}
				}
			]
		});
		prompt.present();
	}

	openFile(item) {
		this.common.openFile(item.type, item.format, item.id, item.name, false, true);
	}
}
